﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Diagnostics;

namespace TJ_MainNameSpace
{
    public partial class FormLogon : Form
    {
        public FormLogon()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
        }

        MySqlConnection con = new MySqlConnection(global::TongJi_EMS.Properties.Settings.Default.monitorConnection);

        private void button1_Click(object sender, EventArgs e)
        {
            int l_user_id = 0, l_user_privilege = -1;
            string /*user_right = "",*/  l_user_name = this.textBox1.Text, l_user_password = "oiuyeywlg;";
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            string user = @"select user_id,user_right,password from user where user_name='" + l_user_name + "'";
            MySqlCommand cmd = new MySqlCommand(user, con);
            System.Data.Common.DbDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                l_user_id = reader.GetInt16(0);
                l_user_privilege = reader.GetInt16(1);
                l_user_password = reader.GetString(2);
            }
            if (l_user_privilege == -1)
            {
                MessageBox.Show("请输入有效的用户名！");
                EventLog.WriteEntry("TJ_EMS_EventLog", "用户" + this.textBox1.Text + "登录失败", EventLogEntryType.Warning);
                reader.Close();
                con.Close();
                return;
            }

            if (this.textBox2.Text == l_user_password && this.textBox1.Text != "×&￥（￥%￥）TJEIFEH")
            {
                //登录成功
                EventLog.WriteEntry("TJ_EMS_EventLog", "用户" + this.textBox1.Text + "登录成功", EventLogEntryType.SuccessAudit);
                TongJi_EMS._Management.UserManager.Instance.UserLogon = true;
                Yonghu_Name = this.textBox1.Text;
                TongJi_EMS._Management.UserManager.Instance.LogOn(l_user_id, l_user_name, l_user_privilege);
                global::TongJi_EMS.Properties.Settings.Default.autologon = this.checkBox1.Checked;
                global::TongJi_EMS.Properties.Settings.Default.Save();
                reader.Close();
                con.Close();
                this.Close();
                DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show("请输入有效的密码");
                EventLog.WriteEntry("TJ_EMS_EventLog", "用户" + this.textBox1.Text + "登录失败", EventLogEntryType.Warning);
                reader.Close();
                con.Close();
            }

            //else
            //{
            //    MessageBox.Show("请输入有效的用户名和密码");
            //    EventLog.WriteEntry("TJ_EMS_EventLog", "！！非法用户登录", EventLogEntryType.Error);
            //}

        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "";
            this.textBox2.Text = "";
        }

        private void Logon_Load(object sender, EventArgs e)
        {
            this.checkBox1.Checked = global::TongJi_EMS.Properties.Settings.Default.autologon;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}